<template>
  <div class="data-wrap">
    <my-tabs :tab-list="tabList">
      <template v-slot="{ row }">
        <li
          class="item pointer"
          :class="{ active: routePath === row.path }"
          @click="handleClickMenu(row)"
        >
          {{ row.title }}
        </li>
      </template>
    </my-tabs>
    <el-upload
      class="avatar-uploader"
      ref="upload"
      action=""
      list-type="picture-card"
      :show-file-list="false"
      :auto-upload="false"
      :on-change="changeUpload"
    >
      <i class="el-icon-plus avatar-uploader-icon"></i>
      <div class="el-upload__tip" slot="tip">
        建议选择清晰、尺寸为300px*300px照片作为头像<br />支持JPG,JPGE,PNG格式，大小限制2MB以内
      </div>
    </el-upload>
    <template v-if="fileShow">
      <el-row type="flex" class="cropper-content">
        <div class="cropper">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :fixed="option.fixed"
            :fixedNumber="option.fixedNumber"
            :centerBox="option.centerBox"
            :infoTrue="option.infoTrue"
            :fixedBox="option.fixedBox"
            @real-time="realTime"
          ></vueCropper>
        </div>
        <div :style="previews.div" class="preview">
          <img :src="previews.url" :style="previews.img" />
        </div>
      </el-row>
    </template>
    <el-button class="btn" size="mini" type="primary" @click="finish">上传</el-button>
  </div>
</template>
<script>
import { VueCropper } from 'vue-cropper'
import MyTabs from '../components/myTab.vue'
export default {
  components: {
    VueCropper,
    MyTabs
  },
  inject: ['reload'],
  data() {
    return {
      previews: {},
      option: {
        img: '', // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        size: 0.8, // 裁剪生成图片的质量
        outputType: 'jpeg', // 裁剪生成图片的格式
        canScale: true, // 图片是否允许滚轮缩放
        autoCrop: true, // 是否默认生成截图框
        fixedBox: true, // 固定截图框大小 不允许改变
        fixed: true, // 是否开启截图框宽高固定比例
        fixedNumber: [1, 1], // 截图框的宽高比例
        full: true, // 是否输出原图比例的截图
        canMoveBox: false, // 截图框能否拖动
        original: false, // 上传图片按照原始比例渲染
        centerBox: true, // 截图框是否被限制在图片里面
        infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },
      filename: '',
      fileShow: false,
      tabList: [
        {
          path: '/my/profile/my_profile',
          title: '基本信息'
        },
        {
          path: '/my/profile/my_photo',
          title: '头像设置'
        }
      ],
      fileList: [],
      infoForm: {
        avatar: 0
      }
    }
  },
  computed: {
    routePath() {
      return this.$route.path
    }
  },
  methods: {
    changeUpload(file, fileList) {
      this.filename = file.name
      const isLt2M = file.size / 1024 / 1024 < 2
      const isIMG = ['.jpeg', '.jpg', '.png'].some(r => file.name.endsWith(r))
      if (!isIMG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 2MB!')
        return false
      }
      // 上传成功后将图片地址赋值给裁剪框显示图片
      this.$nextTick(() => {
        this.fileShow = true
        this.option.img = URL.createObjectURL(file.raw)
      })
    },
    realTime(data) {
      this.previews = data
    },
    finish() {
      if (!this.filename) return
      this.$refs.cropper.getCropBlob(async data => {
        let frm = new FormData()
        frm.append('file', data, this.filename)
        this.$api.uploadPutFile(
          frm,
          res => {
            this.onSubmit(res.data.id)
          },
          error => {
            this.$message.error(error.msg)
          }
        )
      })
    },
    onSubmit(id) {
      let _this = this
      _this.$api.editAccountInfo(
        {
          avatar: id
        },
        res => {
          _this.$message.success(res.msg, res => {
            this.$store.dispatch('user/userInfo') // 更新用户信息
            this.reload()
          })
        },
        error => {
          _this.$message.error(error.msg)
        }
      )
    },
    handleClickMenu(data) {
      this.$router.push({
        path: data.path
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 110px;
    height: 110px;
    line-height: 110px;
    text-align: center;
  }
}
// 截图
.cropper-content {
  margin-top: 20px;
  .cropper {
    width: 300px;
    height: 335px;
  }
  .preview {
    overflow: hidden;
    margin-left: 50px;
  }
}
.btn {
  margin-top: 20px;
  padding: 8px 32px;
}
</style>
